from datetime import datetime

from itpay.utils import maestro_tracker
from .models import Account


def migrate_mch_create(cursor=None):
    """
    商户充值
    :return:
    """

    g = 0
    while True:
        mchs = Account.objects.filter().order_by('id')
        if cursor:
            mchs = mchs.filter(id__gt=cursor)
        i = 0
        for o in mchs[:500]:
            i += 1
            g += 1
            cursor = o.id
            try:
                maestro_tracker.track_maestro_mch_create(o.id, int(datetime.strftime(o.created_at, '%s')))
            except:
                print('migrate_mch_create: {}'.format(o.id))
            if i % 100 == 0:
                print('migrate_mch_create: ' + str(g))

        if i == 0:
            break
